//
//  HBProtocolDispatcherViewController.m
//  HBLibary_Example
//
//  Created by Rambo on 2019/3/1.
//  Copyright © 2019 Rambo. All rights reserved.
//

#import "HBProtocolDispatcherViewController.h"
#import "HBProtocolDispatcher.h"




@protocol HBProtocolDispatcherToolDelegate <NSObject>

- (void)toolTime;

@end

@interface HBProtocolDispatcherTool : NSObject

@property (nonatomic, weak) id<HBProtocolDispatcherToolDelegate> delegate;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) HBProtocolDispatcherWeakArray *delegates;

- (void)addDelegate:(id<HBProtocolDispatcherToolDelegate>)delegate;

@end

static HBProtocolDispatcherTool *tool;
static dispatch_once_t onceToken;

@implementation HBProtocolDispatcherTool

+ (instancetype)shareInstance{
    
    dispatch_once(&onceToken, ^{
        if (tool == nil) {
            tool = [[self alloc] init];
        }
    });
    return tool;
}

- (void)start{
    if (_timer == nil) {
        [self.timer fire];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(toolTime)]) {
        [self.delegate toolTime];
    }
}

- (void)deallocShareIntance{
    [_timer invalidate];
    _timer = nil;
    tool = nil;
    _delegates = nil;
    onceToken = 0;
}

- (void)addDelegate:(id<HBProtocolDispatcherToolDelegate>)delegate{
    if (delegate) {
        BOOL isExist = NO;
        for (id subDelegate in [self.delegates allObjects]) {
            if (subDelegate == delegate) {
                isExist = YES;
                break;
            }
        }
        if (!isExist) {
            [self.delegates addObject:delegate];
            self.delegate = [HBProtocolDispatcher dispatcherProtocol:@protocol(HBProtocolDispatcherToolDelegate) toImplemertors:self.delegates];
        }
    }
}

- (void)removeDelegate:(id)delegate{
    if (delegate) {
        BOOL isExist = NO;
        for (id subDelegate in [self.delegates allObjects]) {
            if (subDelegate == delegate) {
                isExist = YES;
                break;
            }
        }
        if (isExist) {
            [self.delegates removeObject:delegate];
            _delegate = [HBProtocolDispatcher dispatcherProtocol:@protocol(HBProtocolDispatcherToolDelegate) toImplemertors:self.delegates];
        }
    }
}

- (NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(start) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    return _timer;
}

- (HBProtocolDispatcherWeakArray *)delegates{
    if (!_delegates) {
        _delegates = [HBProtocolDispatcherWeakArray new];
    }
    return _delegates;
}


@end



@interface HBProtocolDispatcherViewController ()<HBProtocolDispatcherToolDelegate>

@end

@implementation HBProtocolDispatcherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [[HBProtocolDispatcherTool shareInstance] addDelegate:self];
    [[HBProtocolDispatcherTool shareInstance] start];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    HBProtocolDispatcherViewController *vc = [[HBProtocolDispatcherViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)dealloc{
    NSLog(@"%s",__func__);
}

#pragma mark - HBProtocolDispatcherToolDelegate

- (void)toolTime{
    NSLog(@"%p",self);
}



@end
